package com.apes.autoparts.masterdata.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.masterdata.model.EpcPartsCategory;
import com.apes.autoparts.masterdata.repository.EpcPartsCategoryRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.rop.converter.DateUtils;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @ClassName PartsCategoryService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/11/11 17:15
 * @Version 1.0
 **/
@Service("epcPartsCategoryService")
public class EpcPartsCategoryService extends DomainService {
    @Autowired
    private EpcPartsCategoryRepository epcPartsCategoryRepository;

    @Autowired
    private EpcMasterDataManager epcMasterDataManager;

    public EpcPartsCategory save(SimpleRequest request) {
        EpcPartsCategory category = conserve(request);
        EpcPartsCategory epcPartsCategory = epcPartsCategoryRepository.save(category);
        if (category.getSuperiorId() != null) {
            recursion(epcPartsCategory);
        }
        epcMasterDataManager.synEsb("cm.parts.category.sendSave", MapUtil.mapped(category));
        //同步至老系统
        synPartsCategory(epcPartsCategory, "S");
        return epcPartsCategory;
    }

    public EpcPartsCategory update(SimpleRequest request) {
        EpcPartsCategory category = conserve(request);
        if (category.getSuperiorId() != null) {
            recursion(category);
        }
        epcMasterDataManager.synEsb("cm.parts.category.sendSave", MapUtil.mapped(category));
        //同步至老系统
        synPartsCategory(category, "U");
        return epcPartsCategoryRepository.save(category);
    }

    public EpcPartsCategory conserve(SimpleRequest request) {
        EpcPartsCategory category = request.getO(EpcPartsCategory.class);
        category.setCreatorName(this.getSession(request).getUserName());
        category.setEntryDate(new Date());
        category.setTime(DateUtils.format(category.getEntryDate(), "yyyyMMdd.hhmmss.SSS"));
        return category;
    }

    /**
     * 暂定 从最父级 一条条发送保存信息去新框架
     *
     * @param epcPartsCategory
     */
    public void recursion(EpcPartsCategory epcPartsCategory) {
        //查询父级
        EpcPartsCategory category = epcPartsCategoryRepository.findOne(epcPartsCategory.getSuperiorId());
        if (category.getSuperiorId() != null) {
            recursion(category);
        }
        epcMasterDataManager.synEsb("cm.parts.category.sendSave", MapUtil.mapped(category));
    }


    public String delete(SimpleRequest request) {
        EpcPartsCategory category = request.getO(EpcPartsCategory.class);
        epcPartsCategoryRepository.delete(category);
        return "删除成功";
    }

    public void synPartsCategory(EpcPartsCategory epcPartsCategory, String sign) {
        String method = "S".equals(sign) ? "mdmPlxxService.insert" : "mdmPlxxService.update";
        String TS01 = epcPartsCategory.getTime();
        if ("U".equals(sign)) {
            //更新操作先查询
            TS01 = epcMasterDataManager.getVersion("mdmPlxxService.query", JSONObject.toJSONString(mapper("PLID", epcPartsCategory.getId()))
            );
        }
        //如果是修改  还需要带上原有时间戳
        Map mapper = mapper("PLID", epcPartsCategory.getId(), "PLMC", epcPartsCategory.getName(), "SJID", epcPartsCategory.getSuperiorId(),
                "KZBID", epcPartsCategory.getExtensionId(), "JB", epcPartsCategory.getRank(), "MJBJ", epcPartsCategory.isLastTag() ? 1 : 0,
                "YXBJ", epcPartsCategory.isValid() ? 1 : 0, "PLQM", epcPartsCategory.getCategoryName(), "SSLBID", epcPartsCategory.getTaxId(),
                "SSLBMC", epcPartsCategory.getTaxName(), "SSBL", epcPartsCategory.getTaxRate(), "SB", epcPartsCategory.getTaxType(),
                "FWLXID", epcPartsCategory.getServeCategoryId(), "FWLXMC", epcPartsCategory.getServeCategoryId(), "ZFWLXID", epcPartsCategory.getSubserverCategoryId(),
                "ZFWLXMC", epcPartsCategory.getSubserverCategoryName(), "GWLXID", epcPartsCategory.getStationCreatorId(), "GWLXMC", epcPartsCategory.getStationCreatorName(),
                "SGSC", epcPartsCategory.getConstructionTime(), "CZY", epcPartsCategory.getCreatorName(), "TS01", TS01
        );

        epcMasterDataManager.synErp(method, JSONObject.toJSONString(mapper));
    }
}
